﻿(function () {
  var ie = !!(window.attachEvent && !window.opera);
  var wk = /webkit\/(\d+)/i.test(navigator.userAgent) && RegExp.$1 < 525;
  var fn = [];
  var run = function () {
    for (var i = 0; i < fn.length; i++) fn[i]();
  };
  var d = document;
  d.ready = function (f) {
    if (!ie && !wk && d.addEventListener)
      return d.addEventListener("DOMContentLoaded", f, false);
    if (fn.push(f) > 1) return;
    if (ie)
      (function () {
        try {
          d.documentElement.doScroll("left");
          run();
        } catch (err) {
          setTimeout(arguments.callee, 0);
        }
      })();
    else if (wk)
      var t = setInterval(function () {
        if (/^(loaded|complete)$/.test(d.readyState)) clearInterval(t), run();
      }, 0);
  };
})();

window._Blog = {};

document.ready(function () {
  var _Blog = window._Blog || {};
  const currentTheme = window.localStorage.getItem("theme");
  const isDark = currentTheme === "dark";

  var model_path = "black";

  if (isDark) {
    document.getElementById("switch_default").checked = true;
    // mobile
    document.getElementById("mobile-toggle-theme").innerText = " · Dark";

    model_path = "white";
  } else {
    document.getElementById("switch_default").checked = false;
    // mobile
    document.getElementById("mobile-toggle-theme").innerText = " · Dark";

    model_path = "black";
  }

  _Blog.toggleTheme = function () {
    if (isDark) {
      document.getElementsByTagName("body")[0].classList.add("dark-theme");
      // mobile
      document.getElementById("mobile-toggle-theme").innerText = " · Dark";
    } else {
      document.getElementsByTagName("body")[0].classList.remove("dark-theme");
      // mobile
      document.getElementById("mobile-toggle-theme").innerText = " · Light";
    }

    document
      .getElementsByClassName("toggleBtn")[0]
      .addEventListener("click", () => {
        let isDark = true;

        if (
          document
            .getElementsByTagName("body")[0]
            .classList.contains("dark-theme")
        ) {
          isDark = false;
          document
            .getElementsByTagName("body")[0]
            .classList.remove("dark-theme");
        } else {
          document.getElementsByTagName("body")[0].classList.add("dark-theme");
        }
        if (_Blog.toggleThemeCallback) {
          _Blog.toggleThemeCallback(isDark);
        }
        window.localStorage.setItem(
          "theme",
          document.body.classList.contains("dark-theme") ? "dark" : "light"
        );
      });

    // moblie
    document
      .getElementById("mobile-toggle-theme")
      .addEventListener("click", () => {
        let isDark = true;
        if (
          document
            .getElementsByTagName("body")[0]
            .classList.contains("dark-theme")
        ) {
          isDark = false;
          document
            .getElementsByTagName("body")[0]
            .classList.remove("dark-theme");
          // mobile
          document.getElementById("mobile-toggle-theme").innerText = " · Light";
        } else {
          document.getElementsByTagName("body")[0].classList.add("dark-theme");
          // mobile
          document.getElementById("mobile-toggle-theme").innerText = " · Dark";
        }
        if (_Blog.toggleThemeCallback) {
          _Blog.toggleThemeCallback(isDark);
        }
        window.localStorage.setItem(
          "theme",
          document.body.classList.contains("dark-theme") ? "dark" : "light"
        );
      });
  };
  _Blog.toggleTheme();
});

function mobileBtn() {
  var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
  var mobileMenu = document.getElementById("mobile-menu");
  if (toggleMenu.classList.contains("active")) {
    toggleMenu.classList.remove("active");
    mobileMenu.classList.remove("active");
  } else {
    toggleMenu.classList.add("active");
    mobileMenu.classList.add("active");
  }
}

window._Blog.post = function (url, data) {
  return new Promise(function (resolve, reject) {
    try {
      fetch(
        url,
        {
          method: "POST",
          body: JSON.stringify(data),
          headers: {
            "Content-Type": "application/json",
          },
        },
        false
      ).then(function (res) {
        res.json().then(resolve);
      }, reject);
    } catch (error) {
      console.log("fetch error", error);
      reject(error);
    }
  });
};
